package com.android.webframework;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public abstract class DirectionsService extends HTTPService {
	protected final String logTag = "DirSvc";
	protected RequestObject requestOb;
	
	/**
	 * Request a service
	 * @return a response object
	 * @see ResponseObject
	 */
	public abstract ResponseObject request() throws RequestException;
	
	
	/**
	 * Process a JSON request using a provided URL
	 * @return a string
	 * @see String
	 */
	protected final String processJsonRequest() {
		StringBuilder builder = new StringBuilder();
		BufferedReader reader;
		try {
			reader = new BufferedReader(new InputStreamReader(requestHttp((String)requestOb.getParameter("url"))));
			String line;
			while ((line = reader.readLine()) != null) {
				builder.append(line);
			}
			return builder.toString();
		} catch (ObjectNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * Remove HTML from a string
	 * @param a string with HTML formatting
	 * @return a string with no HTML formatting
	 */
	protected final String removeHTML(String htmlString) {
      // Remove HTML tag from java String    
      String noHTMLString = htmlString.replaceAll("\\<.*?\\>", "");

      // Remove Carriage return from java String
      noHTMLString = noHTMLString.replaceAll("\r", "<br/>");

      // Remove New line from java string and replace HTML break
      noHTMLString = noHTMLString.replaceAll("\n", " ");
      noHTMLString = noHTMLString.replaceAll("\'", "&#39;");
      noHTMLString = noHTMLString.replaceAll("\"", "&quot;");
      
      // Replace spaces with plus signs
      noHTMLString = noHTMLString.replaceAll(" ", "+");
      return noHTMLString;
  }
}